/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * <p>Project: jse2204 - Demo
 * <p>Powered by webrx On 2022-10-10 11:12:06
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Demo {
    public static void main(String[] args) throws Exception {
        //正射
        Util u = new Util();
        System.out.println(u.pf(3));

        //反射操作 Book User
        String u2 = "cn.reflection.Util";
        Class<?> c = Class.forName(u2);

        //调用构造方法
        //Constructor cc = c.getConstructor();
        //实例化相当于 new Util()
        //Object o = c.getConstructor().newInstance();

        //实例化相当于 new Util(6)
        Object o = c.getConstructor(int.class).newInstance(6);

        //获取pf方法
        Method pf = c.getMethod("pf",int.class);

        //执行pf方法
        Object n = pf.invoke(o,6); //相当于u.pf(6)
        System.out.println(n);

        System.out.println(c.getMethods().length);
        for (Method method : c.getMethods()) {
            System.out.println(method.getName());
        }
        System.out.println(c.getDeclaredMethods().length);


    }
}
